package org.clipjoint.controller;

import com.alibaba.fastjson.JSON;
import org.clipjoint.entity.Game;
import org.clipjoint.entity.dto.CommentDto;
import org.clipjoint.entity.dto.KeyWordDto;
import org.clipjoint.entity.KeyWord;
import org.clipjoint.enums.GameTypeEnum;
import org.clipjoint.service.impl.keywordAdminServiceImpl;
import org.clipjoint.service.keywordAdminService;
import org.clipjoint.vo.JsonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 陈光耀
 * date: 2021-2-22
 */
@WebServlet("/keyword")
public class KeyWordAdminController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private keywordAdminService keywordService = new keywordAdminServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String method = request.getParameter("method");

        switch (method) {
            case "list":
                list(request, response);
                break;
            case "deleteAll":
                deleteAll(request, response);
                break;
            case "delete":
                delete(request, response);
                break;
            case "add":
                add(request, response);
                break;

        }

    }


    //添加秘钥
    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {

        HttpSession session = request.getSession();

        // 获取请求参数
        Long game_id = Long.valueOf(request.getParameter("game_id"));
        String keyword = request.getParameter("keyword");
        String detail = request.getParameter("detail");


        int result = 0;
        try {
            result = keywordService.add(KeyWord.builder().game_id(game_id).keyword(keyword).detail(detail).build());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 判断结果
        if (result == 1) {
            response.getWriter().print(JsonResult.builder().code(0).msg("添加成功").build().toJsonString());
        } else {
            response.getWriter().print(JsonResult.builder().code(1).msg("添加失败").build().toJsonString());
        }
    }


    //删除一条数据
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {


        // 获取id
        Long id = Long.valueOf(request.getParameter("id"));

        // 调用service删除数据
        int result = 0;
        try {
            result = keywordService.del(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // 判断结果
        if (result == 1) {
            response.getWriter().print(JsonResult.builder().code(0).msg("删除成功").build().toJsonString());
        } else {
            response.getWriter().print(JsonResult.builder().code(1).msg("删除失败").build().toJsonString());
        }
    }


    //表头删除数据
    private void deleteAll(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 获取id数组的json字符串
        String idsJson = request.getParameter("ids");
        // 将json字符串转为List集合
        List<Long> ids = JSON.parseArray(idsJson, Long.class);

        // 调用service删除所有数据
        int result = keywordService.deleteAll(ids);

        // 判断结果
        if (ids.size() == result) {
            response.getWriter().print(JsonResult.builder().code(0).msg("全部删除成功").build().toJsonString());
        } else if (result != 0) {
            response.getWriter().print(JsonResult.builder().code(0).msg("成功删除" + result + "条，失败" + (ids.size() - result) + "条").build().toJsonString());
        } else {
            response.getWriter().print(JsonResult.builder().code(1).msg("删除失败").build().toJsonString());
        }
    }


    // 获取游戏数据列表
    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 获取分页参数
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));

        // 获取排序参数和排序方式
        String orderField = request.getParameter("orderField");
        String orderType = request.getParameter("orderType");


        // 封装dto
        KeyWordDto keywordDto = KeyWordDto.builder().page(page).limit(limit).orderField(orderField).orderType(orderType).build();

        // 调用service获取帖子数据
        List<KeyWordDto> list = null;
        try {
            list = keywordService.list(keywordDto);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // 调用service获取帖子总数据量
        Long count = null;
        try {
            count = keywordService.count(keywordDto);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // 判断结果
        if (list == null || count == null) {
            response.getWriter().print(JsonResult.builder().code(1).msg("查询错误").build().toJsonString());
            return;
        }

        // 封装结果返回
        response.getWriter().print(JsonResult.builder().code(0).count(count).data(list).build().toJsonString());

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
